﻿Namespace Editor.Mobs

    Module Mobs

        Friend Structure MobDef
            Dim ID As Long
            Dim Name As String
            Dim shape As Integer
            Dim Level As Integer
            Dim Avatar As Integer
        End Structure

        Friend lstMobs As SortedDictionary(Of Long, MobDef)

        Friend Structure MobSpawnDef
            Dim ID As Long
            Dim X As Integer
            Dim Y As Integer
            Dim Count As Integer
            Dim RangeX As Integer
            Dim RangeY As Integer
        End Structure

        Friend lstMobSpawns As List(Of MobSpawnDef)

        Friend Const MOB_AnimSpeed = 5000000

        Private objSpawnLock As Object = New Object

        Friend Sub LoadMapMobs()

            lstMobs = New SortedDictionary(Of Long, MobDef)
            lstMobSpawns = New List(Of MobSpawnDef)

            'load mob db
            Dim ssql As String = "SELECT * FROM TBL_MON;"
            Dim arr As ArrayList = DatabaseManager.DB_Game.ExecuteQuery(ssql)

            If arr.Count > 0 Then

                'load mobs into list
                For Each row As Collection In arr

                    Dim tmp As New MobDef
                    tmp.ID = row("ID_MON")
                    tmp.Name = row("STR_NAME")
                    tmp.shape = row("INT2_SHAPE")
                    tmp.Level = row("INT2_LEVEL")
                    tmp.Avatar = row("INT2_AVATAR")

                    lstMobs.Add(tmp.ID, tmp)

                Next

                'load mob spawns for this map
                ssql = "SELECT * FROM GameEvo.dbo.TBL_MONSPAWN WHERE STR_MAPNAME='" & Editor.mCurrentMapFilename & "';"
                arr = DatabaseManager.DB_Game.ExecuteQuery(ssql)

                If arr.Count > 0 Then

                    'load spawns into the list
                    For Each row As Collection In arr

                        Dim tmp As MobSpawnDef

                        tmp.ID = row("ID_MON")
                        tmp.X = row("INT2_X")
                        tmp.Y = row("INT2_Y")
                        tmp.RangeX = row("INT2_RANGEX")
                        tmp.RangeY = row("INT2_RANGEY")
                        tmp.Count = row("INT2_COUNT")

                        lstMobSpawns.Add(tmp)

                    Next

                End If

            Else
                'no mobs in db/error
                MsgBox("Error! found no mobs in the db")
            End If

        End Sub

        Public Sub DrawMobs(mode As DirectX.DrawingMode, X As Integer, Y As Integer, pX As Double, pY As Double, z As Single, Optional HighlightedOnly As Boolean = False)

            'get current tick count
            Dim TickCount As Long = DateTime.Now.Ticks

            Dim spawnD As MobSpawnDef

            SyncLock objSpawnLock

                'iterate through NPC's
                For iT As Integer = 0 To lstMobSpawns.Count - 1

                    spawnD = lstMobSpawns(iT)

                    'check if NPC is at these coordinates
                    If spawnD.X = X And spawnD.Y = Y Then

                        'use current image
                        With Libs.Avatar.Image(lstMobs(spawnD.ID).Avatar)

                            '/////////SELECTED NPCS DIFF

                            'check if we are drawing the selected NPC and if this is the selected one
                            'If (SelectedNPCID = npcD.ID) Then
                            '    If HighlightedOnly Then
                            '        'draw highlighted npc
                            '        DirectX.BlitD3D(.Texture, pX + .Header.OffsetX, pY + .Header.OffsetY, .Header.Width, .Header.Height, z, Editor.arrZoom(Editor.ZoomIndex))
                            '        'check if we should be drawing the npc symbol
                            '        If Editor.bShowSymbols Then
                            '            'draw highlighted npc symbol
                            '            DirectX.BlitD3D(Libs.Editor.Image(2).Texture, ((pX + (Editor.TileWidth / 2)) * Editor.arrZoom(Editor.ZoomIndex)) - 32, ((pY + (Editor.TileHeight / 2)) * Editor.arrZoom(Editor.ZoomIndex)) - 32, Libs.Editor.Image(2).Header.Width, Libs.Editor.Image(2).Header.Height, 0.02F, 1.0F)
                            '        End If
                            '    End If
                            'ElseIf Not HighlightedOnly Then
                            '    'draw npc normally (not selected)
                            '    DirectX.BlitD3D(.Texture, pX + .Header.OffsetX, pY + .Header.OffsetY, .Header.Width, .Header.Height, z, Editor.arrZoom(Editor.ZoomIndex))
                            '    'check if we should be drawing the npc symbol
                            '    If Editor.bShowSymbols Then
                            '        'draw npc symbol
                            '        DirectX.BlitD3D(Libs.Editor.Image(2).Texture, ((pX + (Editor.TileWidth / 2)) * Editor.arrZoom(Editor.ZoomIndex)) - 32, ((pY + (Editor.TileHeight / 2)) * Editor.arrZoom(Editor.ZoomIndex)) - 32, Libs.Editor.Image(2).Header.Width, Libs.Editor.Image(2).Header.Height, 0.02F, 1.0F)
                            '    End If
                            'End If

                            If mode = DirectX.DrawingMode.Normal Then

                                '///////SELECTED NPCS NORMAL
                                If Not HighlightedOnly Then
                                    'draw npc normally (not selected)
                                    DirectX.BlitD3D(.Texture, (pX + (Editor.TileWidth / 2)) - (.Header.Width / 2), (pY + (Editor.TileHeight / 2)) - (.Header.Height / 2), .Header.Width, .Header.Height, z, Editor.arrZoom(Editor.ZoomIndex))
                                    'check if we should be drawing the npc symbol
                                    If Editor.bShowSymbols Then
                                        'draw npc symbol
                                        DirectX.BlitD3D(Libs.Editor.Image(Editor.cWilIdx_Mob).Texture, ((pX + (Editor.TileWidth / 2)) * Editor.arrZoom(Editor.ZoomIndex)) - 32, ((pY + (Editor.TileHeight / 2)) * Editor.arrZoom(Editor.ZoomIndex)) - 32, Libs.Editor.Image(2).Header.Width, Libs.Editor.Image(2).Header.Height, 0.02F, 1.0F)
                                    End If
                                End If

                            ElseIf mode = DirectX.DrawingMode.Text Then

                                DirectX.ProFont9.DrawBorderText(lstMobs(spawnD.ID).Name & vbCrLf & _
                                                                "Level: " & lstMobs(spawnD.ID).Level & vbCrLf & _
                                                                "Count: " & spawnD.Count & vbCrLf & _
                                                                "Range: " & spawnD.RangeX & "x" & spawnD.RangeY, _
                                                                ((pX + (Editor.TileWidth / 2)) * Editor.arrZoom(Editor.ZoomIndex)) - (.Header.Width / 2), ((pY + (Editor.TileHeight / 2)) * Editor.arrZoom(Editor.ZoomIndex)) - (.Header.Height / 2), Color.White, Color.Black, z - 0.01F)

                            End If

                        End With

                        Exit For

                    End If

                Next

            End SyncLock

        End Sub

        Public Sub AddSpawn(MapName As String, id As Long, pX As Integer, pY As Integer, rX As Integer, rY As Integer, Count As Integer)

            Dim ssql As String = "INSERT INTO TBL_MONSPAWN (STR_MAPNAME, ID_MON, INT2_X, INT2_Y, INT2_RANGEX, INT2_RANGEY, INT2_COUNT) VALUES (" & _
                                    MapName & ", " & _
                                    id & ", " & _
                                    pX & ", " & _
                                    pY & ", " & _
                                    rX & ", " & _
                                    rY & ", " & _
                                    Count & ");"

            DatabaseManager.DB_Game.ExecuteNonQuery(ssql)

            Dim tmp As MobSpawnDef

            tmp.ID = id
            tmp.X = pX
            tmp.Y = pY
            tmp.RangeX = rX
            tmp.RangeY = rY
            tmp.Count = Count

            SyncLock objSpawnLock
                lstMobSpawns.Add(tmp)
            End SyncLock

        End Sub

        Public Sub RemoveSpawn(X As Integer, Y As Integer)

            SyncLock objSpawnLock

                For Each spawn As MobSpawnDef In lstMobSpawns

                    If spawn.X = X Then
                        If spawn.Y = Y Then
                            lstMobSpawns.Remove(spawn)

                            Dim ssql As String = "DELETE FROM TBL_MONSPAWN WHERE ID_MON=" & spawn.ID & " AND STR_MAPNAME='" & Editor.mCurrentMapFilename & "' AND INT2_X=" & X & " AND INT2_Y=" & Y & " AND INT2_RANGEX=" & spawn.RangeX & " AND INT2_RANGEY=" & spawn.RangeY & " AND INT2_COUNT=" & spawn.Count & ";"
                            DatabaseManager.DB_Game.ExecuteNonQuery(ssql)

                            Exit For
                        End If
                    End If

                Next

            End SyncLock

        End Sub

        'return npc id from coordinates, -1 = no npc there
        Public Function GetSpawn(X As Integer, Y As Integer) As Integer

            For Each spawn As MobSpawnDef In lstMobSpawns

                If spawn.X = X And spawn.Y = Y Then
                    Return spawn.ID
                End If

            Next

            Return -1

        End Function


    End Module

End Namespace
